package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.DeptService;
import com.zero.clinic.service.JobService;
import com.zero.clinic.service.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {
    @Resource
    private DictionaryService dictionaryService;

    /**
     * @function 进入字典表维护模块
     * @return
     */
    @RequiresPermissions("dictionary:dictionaryPage")
    @RequestMapping("/dictionaryPage")
    public String dictionaryPage(){
        return "dictionaryPage";
    }

    /**
     * @function 字典列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("dictionary:list")
    @RequestMapping("/list")
    public PageInfo<Dictionary> dictionaryList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        System.out.println("pageNum: " + pageNum + ";pageSize: " + pageSize);
        // 0.开始启用PageHelper
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Dictionary信息
        DictionaryExample example = new DictionaryExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据字典名称查询
            DictionaryExample.Criteria criteria1 = example.createCriteria();
            criteria1.andDictionaryNameLike("%"+ keywords +"%");
            // 2.2 根据父字典名称查询
            DictionaryExample.Criteria criteria2 = example.or();
            criteria2.andParentNameLike("%"+ keywords +"%");
        }
        List<Dictionary> dictionaries = dictionaryService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Dictionary> pageInfo = new PageInfo<>(dictionaries);
        return pageInfo;
    }

    /**
     * @function 编辑字典数据页面
     * @param dictionaryId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String dictionaryEdit(Long dictionaryId, Model m){
        // 1.如果前端页面需要更新字典信息，则会传一个需要更新的dictionaryId过来。这里需要通过dictionaryId查出更新信息后共享给前端页面
        if (dictionaryId != null) {
            Dictionary dictionary = dictionaryService.selectByPrimaryKey(dictionaryId);
            m.addAttribute("dictionary",dictionary);
        }

        // 2.将parentId为空的字典数据查出来作为父字典数据，然后共享给前端页面
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdIsNull();
        List<Dictionary> parentDictionaries = dictionaryService.selectByExample(example);
        m.addAttribute("parentDictionaries",parentDictionaries);
        return "dictionaryEdit";
    }

    /**
     * @function 新增字典数据
     * @param dictionary
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("dictionary:insert")
    public MessageObject dictionaryInsert(Dictionary dictionary){
        //1.插入数据
        System.out.println("需要新增的字典数据：" + dictionary);
        int row = dictionaryService.insert(dictionary);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新字典数据
     * @param dictionary
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("dictionary:update")
    public MessageObject dictionaryUpdate(Dictionary dictionary){
       //3.更新数据
        System.out.println("需要更新的字典数据：" + dictionary);
        int row = dictionaryService.updateByPrimaryKeySelective(dictionary);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查字典数据名是否重复
     * @param dictionaryName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkDictionaryName")
    @RequiresPermissions("dictionary:checkDictionaryName")
    public boolean dictionaryCheckDictionaryName(String dictionaryName){
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andDictionaryNameEqualTo(dictionaryName);
        List<Dictionary> dictionaries = dictionaryService.selectByExample(example);
        return dictionaries.size() > 0 ? false : true;
    }

    /**
     * @function 删除字典数据
     * @param dictionaryId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("dictionary:delete")
    public MessageObject dictionaryDelete(Long dictionaryId){
        //1.删除数据前判断该字典数据下面是否还有子数据，如果有则不能删除
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(dictionaryId);
        List<Dictionary> dictionaries = dictionaryService.selectByExample(example);
        if (dictionaries.size() > 0) {
            return MessageObject.createMo(0,"该字典数据还有子数据，不能直接删除");
        }

        //2.根据dictionaryId删除数据
        int row = dictionaryService.deleteByPrimaryKey(dictionaryId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }

    /**
     * @function 批量删除员工
     * @param dictionaryIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("dictionary:batchdel")
    public MessageObject dictionaryBatchDelete(String dictionaryIds){
        //1.删除数据前判断字典数据下面是否还有下属，如果有则不能删除
        String[] dictionaryIdsArr = dictionaryIds.split(",");
        for (String dictionaryId : dictionaryIdsArr) {
            DictionaryExample example = new DictionaryExample();
            DictionaryExample.Criteria criteria = example.createCriteria();
            criteria.andParentIdEqualTo(Long.parseLong(dictionaryId));
            List<Dictionary> dictionaries = dictionaryService.selectByExample(example);
            if (dictionaries.size() > 0) {
                return MessageObject.createMo(0,"被删除的字典数据下还有子数据，不能直接删除");
            }
        }

        //2.根据dictionaryIdsArr删除数据
        int row = dictionaryService.deleteByIds(dictionaryIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }
}
